import java.io.*;
import java.sql.SQLException;

public class SGResponse
{

	private String runcode, authorID, examDate;

	String[] arguments;
	PrintWriter fileOut;
	String fileOutName; 
	final String SITE_BASE_URL = "http://localhost/apegen";
	final String PATH_TO_FILES = "\\tmp\\ape\\";
	
	public SGResponse(String[] argumentsIn) throws SQLException, IOException
	{
		this.arguments = argumentsIn; // this expects arguments that are parsed in the correct order for use by these methods.
		if(argumentsIn[0].equals("generate")) //if it's generate
			generateExam();
		else if(argumentsIn[0].equals("package")) // or maybe package
			packageExam(argumentsIn[1]);
		else if(argumentsIn[0].equals("test")) // otherwise it MUST be test OR...
			testQuestion();
		else
			error(); // we have a problem.
	}
	
	//generate the exam!!!
	private void generateExam()throws SQLException, IOException
	{
	
		SGServlet sg = new SGServlet();
		int examID = sg.execute(arguments);
		generateResponse(examID);
	}

	private void packageExam(String examIDIn)throws SQLException, IOException
	{
		SGServlet sg = new SGServlet();
		sg.execute(arguments);
		fileOutName = "ape-" + examIDIn + ".zip";
	}
	
	private void testQuestion()throws SQLException, IOException
	{
		SGServlet sg = new SGServlet();
		sg.execute(arguments);
		
	}
	
	private void error()
	{
	
	}
	
	private void generateResponse(int examIDIn)
	{
		fileOutName = "response.html"; //set the name of the html file we will

		//open a printwriter to write the file
		try
		{
		fileOut = new PrintWriter(new File(fileOutName));
		}
		catch(Exception e)
		{
			System.out.println("Could not open the filewriter");
		}
		//write the file
		fileOut.print("<META http-equiv=\"refresh\" content=\"0;URL= " + SITE_BASE_URL + "/?page=preview&exam=" + examIDIn + "\" >");
		
		//close the file
		fileOut.close();
		
		
	}
	
	//returns the filename
	public String getFilename()
	{
	
		return fileOutName;
	
	}
}